package com.jftone.bh.app.service;

import com.jftone.bh.app.model.NtbUser;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.Page;
import org.jftone.util.StringUtil;

import java.util.ArrayList;
import java.util.List;


@Service
public class ReferrerService {
	
	private Logger logger = Logger.getLogger(ReferrerService.class);
	
	@Autowired
	private Dao dao;


	public List<IData<String, Object>> queryReferrerList(String userId, String type) throws DaoException {
		List<IData<String, Object>> list = new ArrayList<IData<String, Object>>();
		IData<String, Object> param = new DataMap<String, Object>();

		if(type.equals("1")){
			param.put("P_ID",userId);
			list = dao.query("queryUserListByPid",param);
			for (IData<String, Object> userData:list) {
				param.put("P_ID",userData.getInt("ID"));
				if(dao.get("getUserListByPid",param) != null){
					userData.put("isHave",1);
				}else{
					userData.put("isHave",0);
				}
			}
		}else{
			param.put("id",userId);
			NtbUser user = dao.get(NtbUser.class,param);
			if(user == null){
				return list;
			}
			param.put("USER_ID",user.getPid());
			IData<String, Object> userData = dao.get("getUserByIdForReferrer",param);
			if(userData != null){
				if(StringUtil.isEmpty(userData.getString("PID",""))){
					userData.put("isHave",0);
				}else{
					userData.put("isHave",1);
				}
				list.add(userData);
			}
		}
		return list;
	}
}
